/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JTimeStoreFinder.java
 *
 * Created on 2009-11-22, 14:44:39
 */

package cn.ekuma.epos.inventory;

import cn.ekuma.swing.dialog.AbstractKeyBoardJDialog;
import com.openbravo.pos.base.AppLocal;
import com.openbravo.bean.erp.viewbean.TimeStore_V;
import java.awt.Component;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Window;
import java.util.List;
import javax.swing.AbstractListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

/**
 *
 * @author Administrator
 */
public class JTimeStoreFinder extends AbstractKeyBoardJDialog {

    private TimeStore_V m_sDialogTicket;
    private AbstractListModel model;
    private List<TimeStore_V> tickets;
    private int selectIndex;

    /** Creates new form JUserList */
    private JTimeStoreFinder(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
    }
    /** Creates new form JUserList */
    private JTimeStoreFinder(java.awt.Dialog parent, boolean modal) {
        super(parent, modal);
    }

    public TimeStore_V showUserList(java.util.List<TimeStore_V> atickets) {
        m_sDialogTicket = null;
        if(atickets==null||atickets.size()==0)
            return m_sDialogTicket;
        tickets=atickets;
        model=new AbstractListModel(){
            @Override
            public int getSize() {
                return tickets.size();
            }

            @Override
            public Object getElementAt(int index) {
                return tickets.get(index).getName();
            }
        };
        selectIndex=0;
        jList1.setModel(model);
        jList1.setSelectedIndex(selectIndex);
        setVisible(true);
        return m_sDialogTicket;
    }

    public static JTimeStoreFinder newJDialog(Component ticketsbagshared) {

        Window window = getWindow(ticketsbagshared);
        JTimeStoreFinder mydialog;
        if (window instanceof Frame) {
            mydialog = new JTimeStoreFinder((Frame) window, true);
        } else {
            mydialog = new JTimeStoreFinder((Dialog) window, true);
        }

        mydialog.initComponents();

        mydialog.jScrollPane1.getVerticalScrollBar().setPreferredSize(new Dimension(35, 35));

        return mydialog;
    }

      @Override
    protected void enterKeyProc() {
        m_sDialogTicket = tickets.get(jList1.getSelectedIndex());
        dispose();
    }

    @Override
    protected void escapeKeyProc() {
        dispose();
    }

    @Override
    protected void downKeyProc() {
        if(tickets.size()<=0)
            return;
        selectIndex=jList1.getSelectedIndex();
        if(selectIndex<tickets.size()-1){
            selectIndex++;
        }else{
            selectIndex=0;
        }
         jList1.setSelectedIndex(selectIndex);
    }

    @Override
    protected void upKeyProc() {
        if(tickets.size()<=0)
            return;
        selectIndex=jList1.getSelectedIndex();
        if(selectIndex>0){
            selectIndex--;
        }else{
            selectIndex=tickets.size()-1;
        }
         jList1.setSelectedIndex(selectIndex);
    }

    class MyCellRenderer extends JLabel implements ListCellRenderer {
        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
         String s = value.toString();
         setText(s);
         setFont(new java.awt.Font ("Dialog", 1, 24));
         if (isSelected) {
             setBackground(list.getSelectionBackground());
             setForeground(list.getSelectionForeground());
         } else {
             setBackground(list.getBackground());
             setForeground(list.getForeground());
         }
         setEnabled(list.isEnabled());
         setOpaque(true);
         return this;
        }
 }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jPanel3 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        m_jButtonCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        jPanel1.setLayout(new java.awt.BorderLayout());

        jScrollPane1.setViewportView(jList1);

        jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        jPanel3.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));
        jPanel3.add(jPanel4);

        m_jButtonCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/button_cancel.png"))); // NOI18N
        m_jButtonCancel.setText(AppLocal.getIntString("Button.Close")); // NOI18N
        m_jButtonCancel.setFocusPainted(false);
        m_jButtonCancel.setFocusable(false);
        m_jButtonCancel.setMargin(new java.awt.Insets(8, 16, 8, 16));
        m_jButtonCancel.setRequestFocusEnabled(false);
        m_jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                m_jButtonCancelActionPerformed(evt);
            }
        });
        jPanel3.add(m_jButtonCancel);

        getContentPane().add(jPanel3, java.awt.BorderLayout.SOUTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void m_jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_jButtonCancelActionPerformed

        dispose();
    }//GEN-LAST:event_m_jButtonCancelActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList jList1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton m_jButtonCancel;
    // End of variables declaration//GEN-END:variables

}
